/**
 * Created by 严秋旺 on 2016/3/5.
 */
var longEdit = function () {
}
this.password = function (element) {
    bindInput(element, $('<input type="password" />'), this.save)
}
this.text = function (element) {
    var input = copyAttr(element, $('<input type="text" />'));
    var text = $(element).text();
    if (text != '点击编辑')
        $(input).val(text);
    bindInput(element, input, this.save);
}
this.number = function (element) {
    var text = $(element).text();
    var input = copyAttr(element, $('<input type="number" />'));
    if (text != '点击编辑')
        $(input).val(text);
    bindInput(element, input, this.save);
}
this.select = function (element) {
    var input = copyAttr(element, $('<select><option value="">资源加载中。。。</option></select>'));
    $.ajax({
        url: $(_this).attr('src'), beforeSend: function () {
        }, success: function (rs, s, q) {
            $(input).html(rs);
            var ops = $(input).children();
            for (var i = 0; i < ops.length; i++) {
                if ($(ops).eq(i).text() == $(_this).text())
                    $(ops).eq(i).attr('selected', true);
            }
        }
    });
    bindInput(element, input, this.save);
}
this.textarea = function (element) {
    var text = $(element).text();
    var input = copyAttr(element, $('<textarea>' + text + '</textarea>'));
    bindInput(element, input, this.save)
}
var _this = this;
var e = $('<span class="icon-edit"></span>');
e.click(function (e) {
    $(this).prev().trigger('click');
});
$(obj).after(e);
$(obj).click(function (e) {
    var type = $(this).attr('type');
    _this[type](this);
});
var copyAttr = function (element, input) {
    var attrs = element.attributes;
    for (var i = 0; i < attrs.length; i++) {
        var attr = attrs[i];
        if (LC.regexValidate(/^l_.+/, attr.name)) {
            $(input).attr(attr.name.substr(2), attr.value);
        }
    }
    return input;
};
var bindInput = this.bindInput = function (element, input, save) {
    $(input).blur(function (e) {
        var _this = this;

        function cancel() {
            $(_this).hide();
            $(_this).prev().toggle('slow');
            $(_this).remove();
            return;
        }

        if (($(_this).prev().attr('nullable') == undefined && $(_this).val() == '') || $(_this).val() == $(_this).prev().text()) {
            cancel();
            return;
        }
        if ($(_this).is('select') && $(_this).children(':selected').text() == $(_this).prev().text()) {
            cancel();
            return;
        }
        if (!confirm('确定保存修改吗？')) {
            cancel();
            return;
        }
        var name = $(_this).prev().attr('name');
        var regx = '/^' + $(_this).prev().attr('regx') + '$/';
        var value = $(_this).val();
        var updateId = $(_this).prev().attr('updateId');
        if (updateId != undefined) {
            id = updateId;
        }
        var type = $(_this).prev().attr('type');
        var warm = $(_this).prev().attr('warm');
        if (warm == undefined)
            warm = '该值不合适';
        if (!($(_this).prev().attr('nullable') != undefined && $(_this).val() == '') && !checkValue(regx, value, type)) {
            alert(warm);
            $(_this).focus();
            return false;
        }
        if ($(_this).prev().attr('md5') != undefined)
            value = md5(value);
        $.getJSON(url, [
                {name: LC.const.UPDATE_ID, value: LC.S.id},
                {name: LC.const.UPDATE_NAME, value: name},
                {name: LC.const.UPDATE_VALUE, value: value}
            ],
            function (rs) {
                if (rs.code == code.FINISH || rs.code == code.SUCCESS) {
                    switch ($(_this).prev().attr('type')) {
                        case 'text':
                            $(_this).prev().text(value);
                            break;
                        case 'password':
                            break;
                        case 'select':
                            $(_this).prev().text($(_this).children('[value=' + value + ']').text());
                            break;
                        case 'textarea':
                            $(_this).prev().text(value);
                    }
                    $(_this).hide();
                    $(_this).prev().toggle('slow');
                    $(_this).remove();
                } else {
                    alert(rs.msg);
                }
            });
    });
    $(input).keyup(function (e) {
        if (e.which == 27) {
            $(this).hide();
            $(this).prev().toggle('slow');
            $(this).remove();
            return;
        } else if (e.which == 13) {
            $(this).trigger('blur');
        }
    });
    $(input).hide();
    $(element).hide();
    $(element).after(input);
    $(input).toggle('slow');
    $(input).focus();
}